Skip to content

[ZSTAC-85134] Carry cache pool capacity in KVM responses#3958

Open
zstack-robot-2 wants to merge 1 commit into
feature-5.5.6-local-cachefrom
sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3
Open

[ZSTAC-85134] Carry cache pool capacity in KVM responses#3958
zstack-robot-2 wants to merge 1 commit into
feature-5.5.6-local-cachefrom
sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3

Conversation

@zstack-robot-2
Copy link
Copy Markdown
Collaborator

为缓存池相关 KVM agent response 统一补充容量字段,使 PoolRsp、PoolHealthRsp、PoolCapacityRsp、GCPoolRsp 都能携带当前缓存池容量信息,供 MN 在业务响应中直接刷新容量。

sync from gitlab !9849

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 3935119a-8f22-42d6-827d-7127a27d4089

📥 Commits

Reviewing files that changed from the base of the PR and between 298fc67 and 26925a6.

⛔ Files ignored due to path filters (1)
  • sdk/src/main/java/org/zstack/sdk/HostCacheStoreInventory.java is excluded by !sdk/**
📒 Files selected for processing (7)
  • conf/db/upgrade/V5.5.18__schema.sql
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityInventory.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO_.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventory.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventoryDoc_zh_cn.groovy
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
✅ Files skipped from review due to trivial changes (1)
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO_.java
🚧 Files skipped from review as they are similar to previous changes (4)
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventoryDoc_zh_cn.groovy
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityInventory.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO.java
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java

Walkthrough

新增并持久化主机缓存的物理总/可用容量与系统占用容量字段;更新 HostCacheStore 的 Inventory 与 Capacity VO 映射与查询字段;同步 KVM agent 的池响应 DTO,合并池容量字段到共享基类并加入池健康响应的标识字段。

变更内容

主机缓存容量模型与 KVM Pool DTO

Layer / File(s) Summary
数据库模式:新增物理/系统容量列
conf/db/upgrade/V5.5.18__schema.sql
HostCacheStoreCapacityVO 表新增 totalPhysicalCapacityavailablePhysicalCapacitysystemUsedCapacity 三列(BIGINT NOT NULL DEFAULT 0),并移除原 allocated/dirty 列。
持久化实体与静态元模型
header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO.java, header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO_.java
在 VO 中新增 totalPhysicalCapacityavailablePhysicalCapacitysystemUsedCapacity 字段及其 getter/setter;在静态元模型中添加对应 SingularAttribute
CapacityInventory 与 Inventory 映射更新
header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityInventory.java, header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventory.java
CapacityInventory 替换 allocated/dirty 为物理与系统容量字段,valueOf 映射新增对这三项的赋值;HostCacheStoreInventory 添加可查询字段并公开 getter/setter,移除旧的 allocated/dirty 访问器。
文档(中文)
header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventoryDoc_zh_cn.groovy
新增三项字段的文档描述(类型 long,单位字节,since "5.5.6")。
KVM agent DTO:PoolRsp 与子类调整
plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
PoolRsp 中新增 boxed 容量字段 totalCapacityavailableCapacitytotalPhysicalCapacityavailablePhysicalCapacityPoolHealthRsp 新增 poolUuidmountPointPoolCapacityRspGCPoolRsp 改为继承 PoolRsp(移除子类重复容量字段)。

估计代码审查工作量

🎯 4 (Complex) | ⏱️ ~45 minutes

兔子的诗

🐰 我把容量数搬家去,
物理与系统都写明,
基类统一少重复,
Inventory 映射不朦胧,
小兔鼓掌跳庆贺。


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error, 1 warning)

Check name Status Explanation Resolution
Title check ❌ Error 标题不符合规定格式。要求为 '[scope]: ' 格式,但标题以 '[JIRA-KEY]' 开头,缺少指定的 前缀。 将标题修改为符合格式的形式,例如:'feat[kvm]: Carry cache pool capacity in KVM responses' 或 'refactor[cache]: Add capacity fields to KVM response DTOs'。
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed 拉取请求描述与变更集相关,清晰阐述了功能目标和实现方案。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3

Comment @coderabbitai help to get the list of available commands and usage tips.

@zstack-robot-1
Copy link
Copy Markdown
Collaborator

Comment from yaohua.wu:

Review: MR !9849 — ZSTAC-85134

关联 MR

  • zstack-utility !7053 — KVM agent 在 Pool 类响应中填充新的容量字段
  • premium !13885 — HostCacheStoreBase 通过响应直接刷新 HostCacheStoreCapacityVO,并把显式同步纳入 ChainTask 队列

本 MR 是这套修复的"线协议基线":在 PoolRsp 上新增 total/used/available/allocated/dirty 五个字段,并把 PoolHealthRsp/PoolCapacityRsp/GCPoolRsp 改为 extends PoolRsp。下游(utility 的 schemas.py 和 premium 的 updateCapacityFromResponse)依赖这里的字段名与类型一致。

跨仓一致性核查

字段 zstack (Java) zstack-utility (Python) premium 读取
total Long int | None poolRsp.total
available Long int | None poolRsp.available
allocated Long int | None poolRsp.allocated(带 null 守护)✓
dirty Long int | None poolRsp.dirty(带 null 守护)✓
used Long int | None 不读取(见 Suggestion 2)

PoolCapacityRsp 重构后变成空类(字段全部下沉到 PoolRsp)。由于 JSON 序列化看的是字段名,这与旧 PoolCapacityRsp 直接声明这些字段在线格式上完全等价 — 旧管理节点 ↔ 新 agent、新管理节点 ↔ 旧 agent 都能正确反序列化已有字段,线兼容性 OK

PoolHealthRsp/GCPoolRsp 现在通过继承额外携带 5 个容量字段;旧版客户端反序列化时会忽略未知字段,前向兼容 OK。premium 侧 grayUpgrade.json 已经把新字段统一标注为 5.5.18,符合 ZStack 灰度升级框架要求。

Findings

🟢 Suggestion — 文档化 PoolHealthRsp 的语义扩展

PoolHealthRsp 原本是"聚合健康信号",现在因继承 PoolRsp 自动携带容量。配合 utility 端 _to_pool_health_rsp 在 check_pool 路径里调用 _fill_pool_rsp,这意味着每次健康检查都会回传容量并落库(见 premium 侧 updateCapacityFromResponse 在通用 httpCall 成功路径触发)。这是个语义扩展:PoolHealthRsp 不再只是"健康"。

建议:要么在类上加一行注释说明 health response 也会携带最新容量;要么如果你觉得健康通道不该承担容量同步职责,则在 _to_pool_health_rsp 不调用 _fill_pool_rsp、保持 PoolHealthRsp 容量字段为 null(premium 侧已经按 null 做了短路)。

Coverage

  • 单文件 1 个 hunk,纯线协议变更;本身没有运行时分支
  • 验证策略集中在 utility !7053 + premium !13885 的回归用例
  • Jira ZSTAC-85134 描述的根因(扩容后容量未即时刷新)不在本 MR 直接修,但本 MR 是修复的前提

Verdict: APPROVED

线协议变更最小、对称、向下兼容。无阻塞性问题。


🤖 Robot Reviewer

@MatheMatrix MatheMatrix force-pushed the sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3 branch from 738bef5 to 87ad2f5 Compare May 14, 2026 07:04
@MatheMatrix
Copy link
Copy Markdown
Owner

Comment from haidong.pang:

已调整:PoolHealthRsp 不再继承 PoolRsp,check_pool 响应不携带容量,容量只放在会改变缓存池状态的 pool 操作响应里。used 字段也已经从协议中移除。

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java`:
- Around line 5272-5284: The PoolRsp model in KVMAgentCommands is missing the
'used' capacity field and PoolHealthRsp should extend PoolRsp so health
responses reuse the same capacity carrier; update the PoolRsp class to add a
public Long used; then change PoolHealthRsp to extend PoolRsp (retain its public
Boolean healthy and public String reason fields) so health responses include
poolUuid/mountPoint/total/available/allocated/dirty/used; make the analogous
changes to the other duplicate definitions mentioned (the other
PoolRsp/PoolHealthRsp occurrences).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: 2e9f4772-c784-4d32-9c4b-76a367334d1f

📥 Commits

Reviewing files that changed from the base of the PR and between 738bef5 and 87ad2f5.

📒 Files selected for processing (1)
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java

Comment on lines 5272 to 5284
public static class PoolRsp extends AgentResponse {
public String poolUuid;
public String mountPoint;
public Long total;
public Long available;
public Long allocated;
public Long dirty;
}

public static class PoolHealthRsp extends AgentResponse {
public Boolean healthy;
public String reason;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

缓存池响应模型未完整对齐容量协议(缺 usedPoolHealthRsp 未继承 PoolRsp)。

当前实现会导致容量字段集合不完整,并且健康响应无法复用同一容量载体;这与本次“统一从响应刷新容量”的目标不一致。

建议修改
 public static class PoolRsp extends AgentResponse {
     public String poolUuid;
     public String mountPoint;
     public Long total;
+    public Long used;
     public Long available;
     public Long allocated;
     public Long dirty;
 }

-public static class PoolHealthRsp extends AgentResponse {
+public static class PoolHealthRsp extends PoolRsp {
     public Boolean healthy;
     public String reason;
 }

Also applies to: 5286-5287, 5295-5298

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java` around lines
5272 - 5284, The PoolRsp model in KVMAgentCommands is missing the 'used'
capacity field and PoolHealthRsp should extend PoolRsp so health responses reuse
the same capacity carrier; update the PoolRsp class to add a public Long used;
then change PoolHealthRsp to extend PoolRsp (retain its public Boolean healthy
and public String reason fields) so health responses include
poolUuid/mountPoint/total/available/allocated/dirty/used; make the analogous
changes to the other duplicate definitions mentioned (the other
PoolRsp/PoolHealthRsp occurrences).

@MatheMatrix MatheMatrix force-pushed the sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3 branch 3 times, most recently from 90daf61 to 298fc67 Compare May 18, 2026 08:34
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@conf/db/upgrade/V5.5.18__schema.sql`:
- Around line 22-24: The new column definitions use unquoted identifiers and are
only inside a CREATE TABLE IF NOT EXISTS block, which won't add columns for
existing tables and may conflict with reserved words; update the migration to
wrap each column name in backticks (e.g., `totalPhysicalCapacity`,
`availablePhysicalCapacity`, `systemUsedCapacity`) and add an ALTER TABLE path
for upgrades (use ALTER TABLE ... ADD COLUMN ... with backticks) or implement a
stored procedure that checks for column existence and adds the NOT NULL columns
while backfilling historical rows safely (e.g., add with DEFAULT or add nullable
then UPDATE existing rows then ALTER to NOT NULL), referencing the CREATE TABLE
IF NOT EXISTS, ALTER TABLE and stored procedure approach in the migration.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: b8258f32-b076-4959-b440-3165ba0f3d4d

📥 Commits

Reviewing files that changed from the base of the PR and between 87ad2f5 and 298fc67.

⛔ Files ignored due to path filters (1)
  • sdk/src/main/java/org/zstack/sdk/HostCacheStoreInventory.java is excluded by !sdk/**
📒 Files selected for processing (7)
  • conf/db/upgrade/V5.5.18__schema.sql
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityInventory.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO_.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventory.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventoryDoc_zh_cn.groovy
  • plugin/kvm/src/main/java/org/zstack/kvm/KVMAgentCommands.java
✅ Files skipped from review due to trivial changes (2)
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreCapacityVO_.java
  • header/src/main/java/org/zstack/header/volumeCache/HostCacheStoreInventoryDoc_zh_cn.groovy

Comment on lines +22 to +24
`totalPhysicalCapacity` BIGINT NOT NULL DEFAULT 0,
`availablePhysicalCapacity` BIGINT NOT NULL DEFAULT 0,
`systemUsedCapacity` BIGINT NOT NULL DEFAULT 0,
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot May 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

新增列名需使用反引号包裹,且升级场景未正确处理。

根据编码规范,所有列名必须使用反引号包裹以避免 MySQL 8.0 / GreatSQL 保留关键字冲突。此外,CREATE TABLE IF NOT EXISTS 语句在表已存在时不会添加新列,升级场景需要使用 ALTER TABLE 或存储过程来处理。

建议修改:添加 ALTER TABLE 语句处理升级场景
 CREATE TABLE IF NOT EXISTS `HostCacheStoreCapacityVO` (
     `uuid`              VARCHAR(32)    NOT NULL,
     `totalCapacity`     BIGINT         NOT NULL DEFAULT 0,
     `availableCapacity` BIGINT         NOT NULL DEFAULT 0,
-    `totalPhysicalCapacity`     BIGINT NOT NULL DEFAULT 0,
-    `availablePhysicalCapacity` BIGINT NOT NULL DEFAULT 0,
-    `systemUsedCapacity`        BIGINT NOT NULL DEFAULT 0,
+    `totalPhysicalCapacity`     BIGINT NOT NULL DEFAULT 0,
+    `availablePhysicalCapacity` BIGINT NOT NULL DEFAULT 0,
+    `systemUsedCapacity`        BIGINT NOT NULL DEFAULT 0,
     PRIMARY KEY (`uuid`),
     ...
 ) ENGINE = InnoDB DEFAULT CHARSET = utf8;

+-- Handle upgrade scenario: add new columns if table already exists
+CALL AddColumnIfNotExists('zstack', 'HostCacheStoreCapacityVO', 'totalPhysicalCapacity', 'BIGINT NOT NULL DEFAULT 0');
+CALL AddColumnIfNotExists('zstack', 'HostCacheStoreCapacityVO', 'availablePhysicalCapacity', 'BIGINT NOT NULL DEFAULT 0');
+CALL AddColumnIfNotExists('zstack', 'HostCacheStoreCapacityVO', 'systemUsedCapacity', 'BIGINT NOT NULL DEFAULT 0');

As per coding guidelines: "所有表名和列名必须使用反引号包裹" 以及 "When NOT NULL exists, must use stored procedure or other functions to process historical data"。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@conf/db/upgrade/V5.5.18__schema.sql` around lines 22 - 24, The new column
definitions use unquoted identifiers and are only inside a CREATE TABLE IF NOT
EXISTS block, which won't add columns for existing tables and may conflict with
reserved words; update the migration to wrap each column name in backticks
(e.g., `totalPhysicalCapacity`, `availablePhysicalCapacity`,
`systemUsedCapacity`) and add an ALTER TABLE path for upgrades (use ALTER TABLE
... ADD COLUMN ... with backticks) or implement a stored procedure that checks
for column existence and adds the NOT NULL columns while backfilling historical
rows safely (e.g., add with DEFAULT or add nullable then UPDATE existing rows
then ALTER to NOT NULL), referencing the CREATE TABLE IF NOT EXISTS, ALTER TABLE
and stored procedure approach in the migration.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment from haidong.pang:

未发布功能,不考虑该场景

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zstack-robot-2,明白了,既然 HostCacheStoreCapacityVO 是尚未发布的新表,不存在需要升级的历史数据,CREATE TABLE IF NOT EXISTS 方式完全可行,无需额外的 ALTER TABLE 升级路径。感谢说明!

(^・ω・^ )


🧠 Learnings used
Learnt from: zstack-robot-2
Repo: MatheMatrix/zstack PR: 3416
File: conf/db/upgrade/V5.5.12__schema.sql:24-35
Timestamp: 2026-03-05T02:13:23.339Z
Learning: In ZStack, the database schema name is fixed as 'zstack'. For SQL upgrade scripts under conf/db/upgrade, continue using TABLE_SCHEMA = 'zstack' and reference tables as zstack.`UsedIpVO` (i.e., qualify with the schema). Do not replace with DATABASE() or remove the schema qualifier for portability. This pattern can be assumed across upgrade scripts in this directory.

Learnt from: zstack-robot-2
Repo: MatheMatrix/zstack PR: 3891
File: conf/db/upgrade/V5.5.22__schema.sql:28-31
Timestamp: 2026-05-07T09:44:01.429Z
Learning: In ZStack MySQL upgrade scripts under conf/db/upgrade/*.sql, if a table already contains a TIMESTAMP column defined with ON UPDATE CURRENT_TIMESTAMP (e.g., lastOpDate), then any second TIMESTAMP column (e.g., createDate) must NOT use DEFAULT CURRENT_TIMESTAMP. This repository’s CI/validated environment uses MySQL 5.7 with explicit_defaults_for_timestamp=OFF, which can trigger MySQL Error 1293 (only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT/ON UPDATE). Use a non-CURRENT_TIMESTAMP sentinel default such as '2000-01-01 00:00:00' (and typically keep it NOT NULL), and rely on ModelServiceRefVO.PrePersist in Java to set the real creation timestamp for new JPA-managed rows.

Add cache pool capacity fields to mutating pool-related KVM agent response DTOs so MN can refresh capacity from operation responses.

Resolves: ZSTAC-85134

Change-Id: I04b8c898a8fcc213f025a4db848e2f4cb97689f5
@MatheMatrix MatheMatrix force-pushed the sync/haidong.pang/codex/ZSTAC-85134-refresh-cache-pool-capacity@@3 branch from 298fc67 to 26925a6 Compare May 18, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants